package com.victor.zjbj_android.mod.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.victor.zjbj_android.R;
import com.victor.zjbj_android.bean.RequestBean;
import com.victor.zjbj_android.mod.login.LoginActivity;
import com.victor.zjbj_android.utils.FastJsonTools;
import com.victor.zjbj_android.utils.ZjbjManager;
import com.victor.zjbj_android.utils.retrofit.BaseSubscriber;
import com.victor.zjbj_android.utils.retrofit.ExceptionHandle;
import com.victor.zjbj_android.utils.retrofit.RetrofitUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ForgetPwdActivity extends AppCompatActivity {
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.et_idcard)
    EditText etIdcard;
    @BindView(R.id.rl_sfz)
    RelativeLayout rlSfz;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.rl_xm)
    RelativeLayout rlXm;
    @BindView(R.id.iv4)
    ImageView iv4;
    @BindView(R.id.et_gh)
    EditText etGh;
    @BindView(R.id.rl_gh)
    RelativeLayout rlGh;
    @BindView(R.id.iv5)
    ImageView iv5;
    @BindView(R.id.et_sjh)
    EditText etSjh;
    @BindView(R.id.rl_sjh)
    RelativeLayout rlSjh;
    @BindView(R.id.rl_dierbu)
    RelativeLayout rlDierbu;
    @BindView(R.id.iv6)
    ImageView iv6;
    @BindView(R.id.et_pwd1)
    EditText etPwd1;
    @BindView(R.id.rl_mm1)
    RelativeLayout rlMm1;
    @BindView(R.id.iv7)
    ImageView iv7;
    @BindView(R.id.et_mm2)
    EditText etMm2;
    @BindView(R.id.rl_mm2)
    RelativeLayout rlMm2;
    @BindView(R.id.rl_last)
    RelativeLayout rlLast;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    private int state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        state=0;
        initViews();
    }

    private void initViews() {
        rlHead.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if(state==2){
                      rlSfz.setVisibility(View.GONE);
                      rlDierbu.setVisibility(View.VISIBLE);
                      rlLast.setVisibility(View.GONE);
                      state = 1;

                      btnCommit.setBackgroundResource(R.drawable.next);
                  }
                  else if(state == 1)
                  {
                      rlSfz.setVisibility(View.VISIBLE);
                      rlDierbu.setVisibility(View.GONE);
                      rlLast.setVisibility(View.GONE);
                      state = 0;
                  }


              }
          });
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(state==0){

                    if(StringUtils.isEmpty(etIdcard.getText().toString())){
                        ToastUtils.showShort("身份证号码不能为空");
                        return;
                    }

                    rlSfz.setVisibility(View.GONE);
                    rlDierbu.setVisibility(View.VISIBLE);
                    rlLast.setVisibility(View.GONE);
                    state=1;
                }else if (state==1){

                    if(StringUtils.isEmpty(etName.getText().toString()) && StringUtils.isEmpty(etGh.getText().toString())){
                        ToastUtils.showShort("真实姓名和工号不能为空");
                        return;
                    }

                    if(StringUtils.isEmpty(etSjh.getText().toString())){
                        ToastUtils.showShort("手机号码不能为空");
                        return;
                    }

                    rlSfz.setVisibility(View.GONE);
                    rlDierbu.setVisibility(View.GONE);
                    rlLast.setVisibility(View.VISIBLE);

                    btnCommit.setBackgroundResource(R.drawable.save);
                    state=2;
                }else if (state==2) {
                    if(StringUtils.isEmpty(etPwd1.getText().toString()) || StringUtils.isEmpty(etMm2.getText().toString()) ){
                        ToastUtils.showShort("新密码不能为空");
                        return;
                    }

                    RetrofitUtils mRetrofit1 = RetrofitUtils.getInstance(getApplicationContext()).createBaseApi();

                    final JSONObject json=new JSONObject();
                    try {
                        json.put("newpassword",etPwd1.getText().toString().trim());
                        json.put("re_newpassword",etMm2.getText().toString().trim());
                        json.put("shenfenid",etIdcard.getText().toString().trim());
                        json.put("realname",etName.getText().toString().trim());
                        json.put("gonghao",etGh.getText().toString().trim());
                        json.put("mobile",etSjh.getText().toString().trim());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    ZjbjManager tzsbManager=new ZjbjManager();
                    LogUtils.e(json.toString());
                    if(mRetrofit1!=null){
                        LogUtils.e("aaa");
                    }
                    tzsbManager.userForgetPassword(json.toString(), mRetrofit1, new BaseSubscriber() {
                        @Override
                        public void onError(ExceptionHandle.ResponeThrowable e) {
                            LogUtils.e("bb",e.message);
                        }

                        @Override
                        public void onSuccess(JSONObject jsonObject) throws JSONException {

                            RequestBean bean= FastJsonTools.getBean(jsonObject.toString(),RequestBean.class);

                            if(bean!=null){
                                if(bean.getCode().equals("0")){

                                    ToastUtils.showShort("密码修改成功，请立即登录!");

                                    startActivity(new Intent(ForgetPwdActivity.this,LoginActivity.class));
                                    finish();
                                }
                                else if(bean.getCode().equals("2"))
                                {
                                    ToastUtils.showShort(bean.getInfo().toString());
                                }
                            }

                        }

                    });

                }

            }
        });

    }
}
